package com.ufgov.agcfs.report.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ufgov.agcfs.baseData.dao.ReportTypeDao;
import com.ufgov.agcfs.baseData.entity.ReportType;
import com.ufgov.agcfs.pub.UUID;
import com.ufgov.agcfs.report.dao.ReoprtFieldDao;
import com.ufgov.agcfs.report.dao.ReportDao;
import com.ufgov.agcfs.report.entity.Report;
import com.ufgov.agcfs.report.entity.ReportField;
import com.ufgov.agcfs.report.entity.RptTypeNode;

@Service
public class ReportDesignService {
	
	@Autowired
	public ReportDao reportDao;
	@Autowired
	public ReportTypeDao reportTypeDao;
	@Autowired
	public ReoprtFieldDao reportFieldDao;

	/*public List<Report> getReportList(int start,int length,
			Map<String,String> columnHash,Map<String,String> sortHash,
			Map<String,String> condHash){
		String order = "";
		for (Entry<String, String> entry : sortHash.entrySet()) {
			String key = entry.getKey();
			String value = entry.getValue();
			if (columnHash.containsKey(key)){
				String column = columnHash.get(key);
				order = ","+column+" "+ value+order;
			}
		}
		
		if (!order.equals("")){
			order = order.substring(1);
			order = "order by " + order;
		}
		
		HashMap<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("start", start);
		searchMap.put("endrow", start+length);
		searchMap.put("order",order);
		searchMap.putAll(condHash);
		
		return reportDao.getReportList(searchMap);
	}*/
	
	public List<Report> getReportList(Map<String,String> condHash){
		return reportDao.getReportList(condHash);
	}
	
	public List<RptTypeNode> getRptTypeNodes(){
		List<RptTypeNode> nodes = new ArrayList<RptTypeNode>();
		List<ReportType> reportTypeList = reportTypeDao.findAllReportType();
		String nd="";
		for (ReportType reportType : reportTypeList) {
			if (!reportType.getNd().equals(nd)){
				nd = reportType.getNd();
				RptTypeNode node = RptTypeNode.createNode(nd,nd,"",nd, true, "#", "root");
				nodes.add(node);
			}
			
			RptTypeNode node = RptTypeNode.createNode(nd+reportType.getReportType(), reportType.getNd(),
					reportType.getReportType(),reportType.getReportTypeName(), false, nd, "child");
			nodes.add(node);
		}
		return nodes;
	}
	
	public List<RptTypeNode> getRptTypeByND(String nd){
		List<RptTypeNode> nodes = new ArrayList<RptTypeNode>();
		List<ReportType> reportTypeList = reportTypeDao.findAllReportType();
		for (ReportType reportType : reportTypeList) {
			if (reportType.getNd().equals(nd)){
				RptTypeNode node = RptTypeNode.createNode(nd+reportType.getReportType(), reportType.getNd(),
						reportType.getReportType(),reportType.getReportTypeName(), false, nd, "child");
				nodes.add(node);
			}
		}
		return nodes;
	}
	
	@Transactional
	public void saveReport(Report report,Map<String,String> resultMap){
		if ((report.getId()==null)||(report.getId().equals(""))) {
			Map<String,String> searchMap = new HashMap<String, String>();
			searchMap.put("nd",report.getNd());
			searchMap.put("reportCode", report.getReportCode());
			if (reportDao.getReportList(searchMap).size()>0){
				getErrorMsg(resultMap,"saveError","报表已存在，请修改报表信息！");
				return;
			}
			report.setId(UUID.randomUUID().toString());
			reportDao.insertReport(report);
		} else {
			Map<String,String> searchMap = new HashMap<String, String>();
			searchMap.put("id",report.getId());
			Report rpt = reportDao.getReportList(searchMap).get(0);
			if (rpt.equals(report)){
				reportDao.updateReport(report);
			} else {
				getErrorMsg(resultMap,"saveError","报表信息与系统中不一致！");
			}
		}	
	}
	
	@Transactional
	public void deleteReports(List<String> reptIds,Map<String, String> resultMap){
		for (String id : reptIds) {
			reportDao.deleteReport(id);
		}
	}
		
	public List<ReportField> getRptFldList(Report report){
		return reportFieldDao.getRptFldList(report);
	}
	
	private void getErrorMsg(Map<String, String> resultMap,String flg, String reason) {
		resultMap.put("result", "fail");
		resultMap.put(flg, reason);
		resultMap.put("reason", "保存失败！");
	}
	
	
	
}
